package com.sakura.mall.order.orderItem.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.order.orderItem.domain.entity.OrderItemEntity;
import com.sakura.mall.order.orderItem.domain.request.AddOrderItemRequest;
import com.sakura.mall.order.orderItem.domain.request.UpdateOrderItemRequest;
import com.sakura.mall.order.orderItem.domain.request.OrderItemPageRequest;
import com.sakura.mall.order.orderItem.domain.vo.OrderItemVo;
import com.sakura.mall.order.orderItem.mapper.OrderItemMapper;
import com.sakura.mall.order.orderItem.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单项信息 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItemEntity> implements OrderItemService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddOrderItemRequest request) {
        // todo 其他校验逻辑
        OrderItemEntity orderItem = request.convert(OrderItemEntity.class);
        this.save(orderItem);
    }

    @Override
    public Page<OrderItemVo> queryPage(OrderItemPageRequest request) {
        IPage<OrderItemEntity> qp = request.toPage();
        LambdaQueryWrapper<OrderItemEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<OrderItemVo> ret = qp.convert(entity -> entity.convert(OrderItemVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateOrderItemRequest request) {
        OrderItemEntity entity = request.convert(OrderItemEntity.class);
        entity.setId(id);
        this.updateById(entity);
    }

}

